﻿Public Class GridColumn : Inherits DataGridViewColumn

	Enum ContentType
		Text
		Money
		Combo
		[Date]
	End Enum

	Property Type As ContentType
	Property DataSource As Recordset

	Public Sub New(Type As ContentType)
		MyBase.New(New GridCell)
		Me.Type = Type
	End Sub

End Class

Class GridCell : Inherits DataGridViewTextBoxCell

	ReadOnly Property Type As GridColumn.ContentType
		Get
			If OwningColumn Is Nothing Then Return GridColumn.ContentType.Text
			Return CType(OwningColumn, GridColumn).Type
		End Get
	End Property

	Public Overrides Sub PositionEditingControl(ByVal setLocation As Boolean, ByVal setSize As Boolean, ByVal cellBounds As System.Drawing.Rectangle, ByVal cellClip As System.Drawing.Rectangle, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal singleVerticalBorderAdded As Boolean, ByVal singleHorizontalBorderAdded As Boolean, ByVal isFirstDisplayedColumn As Boolean, ByVal isFirstDisplayedRow As Boolean)
		Dim R As Rectangle = cellBounds
		R.Width -= 1
		R.Height -= 1
		MyBase.PositionEditingControl(setLocation, setSize, R, cellClip, cellStyle, False, False, isFirstDisplayedColumn, isFirstDisplayedRow)
	End Sub

	Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
		MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
		Select Case Type
			Case GridColumn.ContentType.Money
				Dim ctl As GridMoneyBox = CType(DataGridView.EditingControl, GridMoneyBox)
				ctl.Padding = New Padding(0)
				ctl.Value = CType(If(Me.Value Is Nothing, Me.DefaultNewRowValue, Me.Value), Decimal)
			Case GridColumn.ContentType.Text
				Dim ctl As GridTextBox = CType(DataGridView.EditingControl, GridTextBox)
				ctl.Padding = New Padding(0)
				ctl.BackColor = Color.Red
				Select Case dataGridViewCellStyle.Alignment
					Case DataGridViewContentAlignment.BottomCenter, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.TopCenter : ctl.TextAlign = HorizontalAlignment.Center
					Case DataGridViewContentAlignment.BottomRight, DataGridViewContentAlignment.MiddleRight, DataGridViewContentAlignment.TopRight : ctl.TextAlign = HorizontalAlignment.Right
					Case Else : ctl.TextAlign = HorizontalAlignment.Left
				End Select
				ctl.Value = CType(If(IsNull(Me.Value), Me.DefaultNewRowValue, Me.Value), String)
			Case GridColumn.ContentType.Combo
				Dim ctl As GridComboBox = CType(DataGridView.EditingControl, GridComboBox)
				ctl.Padding = New Padding(0)
				ctl.Source = CType(OwningColumn, GridColumn).DataSource
				ctl.Value = CType(If(Me.Value Is Nothing, Me.DefaultNewRowValue, Me.Value), Object)
			Case GridColumn.ContentType.Date
				Dim ctl As GridDateBox = CType(DataGridView.EditingControl, GridDateBox)
				ctl.TextAlign = HorizontalAlignment.Center
				ctl.Value = CType(If(Me.Value Is Nothing, Me.DefaultNewRowValue, Me.Value), Object)
		End Select
	End Sub

	Public Overrides ReadOnly Property EditType() As Type
		Get
			Select Case Type
				Case GridColumn.ContentType.Money
					Return GetType(GridMoneyBox)
				Case GridColumn.ContentType.Text
					Return GetType(GridTextBox)
				Case GridColumn.ContentType.Combo
					Return GetType(GridComboBox)
				Case GridColumn.ContentType.Date
					Return GetType(GridDateBox)
			End Select
			Return Nothing
		End Get
	End Property

	Public Overrides ReadOnly Property ValueType() As Type
		Get
			Select Case Type
				Case GridColumn.ContentType.Money
					Return GetType(Decimal)
				Case GridColumn.ContentType.Text
					Return GetType(String)
			End Select
			Return GetType(Object)
		End Get
	End Property

	Public Overrides ReadOnly Property DefaultNewRowValue() As Object
		Get
			Select Case Type
				Case GridColumn.ContentType.Money
					Return 0
				Case GridColumn.ContentType.Text
					Return ""
			End Select
			Return Nothing
		End Get
	End Property

	Protected Overrides Function GetFormattedValue(value As Object, rowIndex As Integer, ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, valueTypeConverter As System.ComponentModel.TypeConverter, formattedValueTypeConverter As System.ComponentModel.TypeConverter, context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object
		If Type = GridColumn.ContentType.Combo Then
			If Not TypeOf value Is String Then Return ""
			Dim Rs = CType(OwningColumn, GridColumn).DataSource
			If Rs Is Nothing Then Return ""
			Return (From r In Rs.Rows Where r(0) = value Select r(1)).FirstOrDefault
		End If
		Return MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
	End Function

End Class


